package com.myzl.common.utils.file;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Administrator
 */
public class FileHandleUtils {

    /**
     * 或者文件夹下所有文件
     * @param path 文件件路径
     * @return 文件名集合
     */
    public static List<String> getFileNames(String path) {
        List<String> fileNames = new ArrayList<>();
        try (Stream<Path> paths = Files.walk(Paths.get(path))) {
            fileNames = paths
                    .filter(Files::isRegularFile)
                    .map(file -> file.getFileName().toString())
                    .collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileNames;
    }


    /**
     * 或者文件夹下所有文件
     * @param path 文件件路径
     * @return 文件名集合
     */
    public static List<String> getOnlyFileNames(String path) {
        List<String> fileNames = new ArrayList<>();
        File file = new File(path);
        if (!file.exists()) {
            return fileNames;
        }
        File[] files = file.listFiles();
        if (null == files){
            return fileNames;
        }
        for (File f : files) {
            if (!f.isDirectory()) {
                fileNames.add(f.getName());
            }
        }
        return fileNames;
    }
}
